﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
using com.cgWorkstudio.BIMP.Library;
using com.cgWorkstudio.BIMP.Library.vo;
using com.shinhwa.win32api;

namespace com.cgWorkstudio.BIMP.Client {
    public partial class CGClientForm:Form {
        [DllImport("user32.dll",EntryPoint = "GetWindowLong",CharSet = CharSet.Auto)]
        public static extern int GetWindowLong(HandleRef hWnd,int nIndex);

        [DllImport("user32.dll",EntryPoint = "SetWindowLong",CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLong(HandleRef hWnd,int nIndex,int dwNewLong);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);
        const int WS_MINIMIZEBOX = 0x00020000;  // Winuser.h中定义  
        const int SC_RESTORE = 0xF120; //还原

        protected override CreateParams CreateParams {
            get {
                CreateParams cp = base.CreateParams;
                cp.Style = cp.Style | WS_MINIMIZEBOX;   // 允许最小化操作  
                return cp;
            }
        }
        public CGClientForm() {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximumSize = SystemInformation.PrimaryMonitorMaximizedWindowSize;
            this.cgAutoShowTimer.Tick += new EventHandler(CGAutoShowTimer_Tick);
            this.SizeChanged += new EventHandler(CGClientForm_SizeChanged);
        }
        // 移动无边框窗体Handler定义
        public void MoveMessengerFormHandler(object sender,MouseEventArgs e) {
            ReleaseCapture();
            SendMessage(this.Handle,WM_MCHITTEST.WM_SYSCOMMAND,WM_MCHITTEST.SC_MOVE + WM_MCHITTEST.HT_CAPTION.ToInt32(),0);
        }
        void CGClientForm_SizeChanged(object sender,EventArgs e) {
            switch(this.WindowState) {
                case FormWindowState.Minimized:
                    this.Opacity = 0;
                    stopOpacity();
                    break;
                case FormWindowState.Normal:
                    this.Opacity = 0;
                    stopOpacity();
                    startOpacity();
                    break;
            }
        }

        public void startOpacity() {
            if(autoShowing == true) {
                return;
            }
            autoShowing = true;
            this.cgAutoShowTimer.Start();
        }
        public void stopOpacity() {
            autoShowing = false;
            this.cgAutoShowTimer.Stop();
        }
        bool autoShowing = false;
        void CGAutoShowTimer_Tick(object sender,EventArgs e) {
            if(Opacity >= 100) {
                cgAutoShowTimer.Stop();
            }
            Opacity += 16.0;
        }

        protected override void OnLoad(EventArgs e) {
            this.Opacity = 0;
            base.OnLoad(e);
            cgAutoShowTimer.Start();

            int WS_SYSMENU = 0x00080000;
            int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
            SetWindowLong(new HandleRef(this,this.Handle),-16,windowLong | WS_SYSMENU);
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            if(DesignMode) {
                return;
            }
            RoundFormPainter.Paint(this,e);         
        }

        protected override void OnShown(EventArgs e) {
            base.OnShown(e);
        }

        private bool moveFlag = false;
        private int x = 0;
        private int y = 0;

        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) {
            if(moveFlag && (e.Button == MouseButtons.Left)) {
                this.SetBounds(Left + e.X - x,Top + e.Y - y,this.Width,this.Height);
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) {
            if(!moveFlag && e.Clicks >= 1) {
                moveFlag = true;
            }
            x = e.X;
            y = e.Y;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) {
            if(moveFlag) {
                moveFlag = false;
            }
            base.OnMouseUp(e);
        }
        protected void doParentAction(CGControllerType type) {
            Form_Controller frmStartup = (Form_Controller)this.Owner;
            switch(type) {
                case CGControllerType.Close:    // 关闭应用系统
                    frmStartup.CG_ExitApp();
                    break;
                case CGControllerType.Login:    // 显示登录界面s
                    frmStartup.CG_showLoginForm();
                    this.Close();
                    break;
                case CGControllerType.Main:     // 显示系统主界面
                    frmStartup.CG_showMainForm();
                    this.Close();
                    break;
            }
        }
    }
}
